package view;

/*
Cours :             LOG120 
Session :           Saison (hiver) 
Groupe :            01 
Projet :            Laboratoire #2
Etudiant(e)(s) :    Jeremy Moreau
                    Alexandre Richard
                    Philippe Pigeon
Code(s) perm. :     MORJ91010106
					RICA10028806 
                    PIGP08058903
Professeur :        Ghizlane El Boussaidi  
Date remise :       2012-03-010  
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controller.CommandeController;

import model.Plat;
import model.Restaurant;
import model.MenuModele;

@SuppressWarnings("serial")
public class MenuView extends AbstractView implements ListSelectionListener, ActionListener
{
	JList lvMenu;
	JButton btAjouter;
	JTextField tfQty;
	
	public MenuView(CommandeController _ctrl)
	{
		super(_ctrl);
		init();
	}
	
	private void init()
	{
		setLayout(null);
		setBounds(140, 10, 120, 220);
		btAjouter = new JButton("Ajouter");
		btAjouter.setBounds(0, 191, 76, 23);
		btAjouter.addActionListener(this);
		btAjouter.setEnabled(false);
		
		tfQty = new JTextField();
		tfQty.setBounds(86, 192, 24, 20);
		tfQty.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				char c = arg0.getKeyChar();
				if(!Character.isDigit(c)) arg0.consume();
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {}
			
			@Override
			public void keyPressed(KeyEvent arg0) {}
		});
		tfQty.setText("1");
		
		//plats = new ArrayList<Plat>();
		//plats.add(new Plat("Default", 0.00));
		
		lvMenu = new JList();
		lvMenu.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lvMenu.addListSelectionListener(this);
		lvMenu.setBounds(0, 0, 120, 180);
		lvMenu.setBorder(new LineBorder(Color.BLACK));
		
		add(lvMenu);
		add(tfQty, BorderLayout.SOUTH);
		add(btAjouter, BorderLayout.SOUTH);
		setVisible(true);
	}
	
	/*private void load()
	{
		lvMenu.setListData(plats.toArray());
	}*/
	

	@Override
	public void update(Observable o, Object arg) 
	{
		lvMenu.setListData(((MenuModele) o).getPlats().toArray());
	}

	@Override
	public void valueChanged(ListSelectionEvent e) 
	{
		if(lvMenu.getSelectedIndex() == -1)
			btAjouter.setEnabled(false);
		else
			btAjouter.setEnabled(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		getCCtrl().ajouterLigneCommande((Plat)lvMenu.getSelectedValue(), Integer.valueOf(tfQty.getText()));
	}
}
